<?php
/**
 * 数据库的相关操作
 * @author yeahfeng
 *
 */
class Database {
	private static $conn;

	//连接数据库
	static function connect($host,$name,$pwd,$db) {
		Database::$conn = mysql_connect($host,$name,$pwd) or die('数据库链接失败！'.mysql_error());
		mysql_select_db($db,Database::$conn);
		mysql_query("set names utf8");
	}
	
	//查询记录数
	static function count($table,$condition="1=1"){
		$sql = "select count(*) from $table where $condition";
		$result = mysql_query($sql,Database::$conn);
		$resultArray = array();
		if($result){
			$resultArray = mysql_fetch_row($result);
		}
		return $resultArray;
	}
	//计算总和
	static function sum($table,$colum,$condition="1=1"){
		$sql = "select sum($colum) from $table where $condition";
		$result = mysql_query($sql,Database::$conn);
		$resultArray = array();
		if($result){
			$resultArray = mysql_fetch_row($result);
		}
		return $resultArray;
	}
	//计算最大值
	static function max($table,$colum,$condition="1=1"){
		$sql = "select max($colum) from $table where $condition";
		$result = mysql_query($sql,Database::$conn);
		$resultArray = array();
		if($result){
			$resultArray = mysql_fetch_row($result);
		}
		return $resultArray;
	}
	//通过关键id查询（返回一维数组）
	static function findById($table,$idName,$id) {
		$sql = "select * from $table where $idName=$id";
		$result = mysql_query($sql,Database::$conn);
		$resultArray = array();
		if($result){
			$resultArray = mysql_fetch_assoc($result);
		}
		return $resultArray;
	}

	//通过组合字段查询（返回唯一的一维数组）
	static function findByField($table,$condition) {
		$sql = "select * from $table where $condition";
		$result = mysql_query($sql,Database::$conn);
		$resultArray = array();
		if($result){
			$resultArray = mysql_fetch_assoc($result);
		}
		return $resultArray;
	}
	
	//查询所有（返回二维数组）
	static function findAll($table,$condition="1=1") {
		$sql = "select * from $table where $condition";
		$result = mysql_query($sql,Database::$conn);
		$resultArray = array();
		if($result){
			while($row=mysql_fetch_assoc($result)){
				$resultArray[]=$row;
			}
		}
		return $resultArray;
	}
	//两个表关联查询
	static function findTwoTab($table1,$table2,$colums,$condition="1=1") {
		$sql = "select $colums from $table1 a,$table2 b where $condition";
		$result = mysql_query($sql,Database::$conn);
		$resultArray = array();
		while($row=mysql_fetch_assoc($result)){
			$resultArray[]=$row;
		}
		return $resultArray;
	}
	//sql查询
	static function find($sql) {
		$result = mysql_query($sql,Database::$conn);
		$resultArray = array();
		if($result){
			while($row=mysql_fetch_assoc($result)){
				$resultArray[]=$row;
			}
		}
		return $resultArray;
	}
	//分页查询
	static function findPage($table,$startRow,$pageSize,$condition="1=1") {
		$sql = "select * from $table where $condition limit $startRow,$pageSize";
		$result = mysql_query($sql,Database::$conn);
		$resultArray = array();
		if($result){
			while($row=mysql_fetch_assoc($result)){
				$resultArray[]=$row;
			}
		}
		return $resultArray;
	}
	
	//插入
	static function insert($sql) {
		$result = mysql_query($sql,Database::$conn) or die('数据插入失败'.mysql_error(Database::$conn));
		return $result;
	}
	
	//更新
	static function update($sql) {
		$result = mysql_query($sql,Database::$conn) or die('数据更新失败'.mysql_error(Database::$conn));
		return $result;
	}
	
	//删除
	static function delete($sql) {
		$result = mysql_query($sql,Database::$conn) or die('数据更新失败'.mysql_error(Database::$conn));
		return $result;
	}
	
	//关闭数据库链接
	static function close() {
		mysql_close(Database::$conn);
	}
	//事务控制：开始
	static function begin(){
		mysql_query("BEGIN");
	}
	//事务控制：提交
	static function commit(){
		mysql_query("COMMIT");
	}
	//事务控制：回滚
	static function rollback(){
		mysql_query("ROLLBACK");
	}
	//事务控制：结束
	static function end(){
		mysql_query("END");
	}

}

?>